/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { screen } from '@testing-library/react';
import * as React from 'react';
import CurrentUserContextProvider from '../../../../app/components/current-user/CurrentUserContextProvider';
import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like';
import { mockComponent } from '../../../../helpers/mocks/component';
import { mockCurrentUser } from '../../../../helpers/testMocks';
import { renderComponent } from '../../../../helpers/testReactTestingUtils';
import { ComponentQualifier } from '../../../../types/component';
import { App } from '../App';

it('should render Empty Overview for Application with no analysis', async () => {
  renderApp({ component: mockComponent({ qualifier: ComponentQualifier.Application }) });

  expect(
    await screen.findByRole('alert', { name: 'provisioning.no_analysis.application' })
  ).toBeInTheDocument();
});

it('should render Empty Overview on main branch with no analysis', async () => {
  renderApp({}, mockCurrentUser());

  expect(
    await screen.findByRole('alert', { name: 'provisioning.no_analysis_on_main_branch.master' })
  ).toBeInTheDocument();
});

it('should render Empty Overview on main branch with multiple branches with bad configuration', async () => {
  renderApp({ branchLikes: [mockBranch(), mockBranch()] });

  expect(
    await screen.findByRole('alert', {
      name: 'provisioning.no_analysis_on_main_branch.bad_configuration.master.branches.main_branch',
    })
  ).toBeInTheDocument();
});

it('should not render for portfolios and subportfolios', () => {
  const rtl = renderApp({
    component: mockComponent({ qualifier: ComponentQualifier.Portfolio }),
  });
  expect(rtl.container).toBeEmptyDOMElement();

  rtl.unmount();

  renderApp({
    component: mockComponent({ qualifier: ComponentQualifier.Portfolio }),
  });
  expect(rtl.container).toBeEmptyDOMElement();
});

function renderApp(props = {}, userProps = {}) {
  return renderComponent(
    <CurrentUserContextProvider currentUser={mockCurrentUser({ isLoggedIn: true, ...userProps })}>
      <App
        hasFeature={jest.fn().mockReturnValue(false)}
        branchLikes={[]}
        branchLike={mockMainBranch()}
        component={mockComponent()}
        {...props}
      />
    </CurrentUserContextProvider>
  );
}
